package com.zhai.boot.service;

import com.github.pagehelper.PageHelper;
import com.zhai.boot.async.log.BizLogAsync;
import com.zhai.boot.async.log.BizLogConstant;
import com.zhai.boot.async.log.BizLogInOut;
import com.zhai.boot.async.log.BizLogUtil;
import com.zhai.boot.common.LogConstant;
import com.zhai.boot.common.ServiceResult;
import com.zhai.boot.common.TableConstant;
import com.zhai.boot.dao.table.DemoDao;
import com.zhai.boot.domain.in.DemoFindIn;
import com.zhai.boot.domain.in.DemoLogIn;
import com.zhai.boot.domain.in.DemoSaveIn;
import com.zhai.boot.domain.json.DemoJson;
import com.zhai.boot.domain.table.DemoTable;
import com.zhai.boot.mq.sender.ZhaiSender;
import com.zhai.boot.util.JSONUtil;
import com.zhai.boot.util.RedisUtil;
import com.zhai.boot.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.zhai.boot.common.Page;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * @package: com.zhai.boot.service
 * @project-name: zhai-boot
 * @description: demo 服务 - 实现
 * @author: Created by 翟凤玺
 * @create-datetime: 2018-7-20 16:23
 */
@Slf4j
@Service
public class DemoServiceImpl implements DemoService {

    private static final String BUSINESS_NAME = "【demo服务】";
    private static final String FIND_METHOD = "（查询）";
    private static final String GRID_METHOD = "（分页查询）";
    private static final String SAVE_METHOD = "（保存）";
    private static final String GET_BY_ID_METHOD = "（id查询）";
    private static final String LOG_METHOD = "（日志）";

    @Autowired
    private DemoDao demoDao;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ZhaiSender sender;
    @Autowired
    BizLogAsync bizLog;

    @Override
    public ServiceResult<Object> find(DemoFindIn in) {
        log.info(BUSINESS_NAME + LogConstant.SERVICE + FIND_METHOD + LogConstant.IN + JSONUtil.toStr(in));
        DemoTable demoMain = new DemoTable();
        List<DemoTable> demoLists = demoDao.getListByMain(demoMain);
        List<DemoJson> demoResultLists = new ArrayList<>();
        for (DemoTable demoTable : demoLists) {
            DemoJson demoJson = JSONUtil.toObj(demoTable.getDemoJson(), DemoJson.class);
            if (demoJson.getName().contains(in.getName()) || demoJson.getAge().equals(in.getAge())) {
                demoResultLists.add(demoJson);
            }
        }
        log.info(BUSINESS_NAME + LogConstant.RESULT + FIND_METHOD + LogConstant.SUCCESS + LogConstant.OUT + JSONUtil.toStr(demoResultLists));
        return ServiceResult.success(demoResultLists);
    }

    @Override
    public ServiceResult<Object> grid() {
        log.info(BUSINESS_NAME + LogConstant.SERVICE + GRID_METHOD);
        DemoTable demoMain = new DemoTable();
        com.github.pagehelper.Page<Object> page = PageHelper.startPage(TableConstant.DEFAULT_PAGE_NUM, TableConstant.DEFAULT_PAGE_SIZE);
        List<DemoTable> demoLists = demoDao.getListByMain(demoMain);
        log.info(BUSINESS_NAME + LogConstant.RESULT + GRID_METHOD + LogConstant.SUCCESS + LogConstant.OUT + JSONUtil.toStr(demoLists));
        return ServiceResult.success(Page.result(page));
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ServiceResult<Object> save(DemoSaveIn in) {
        log.info(BUSINESS_NAME + LogConstant.SERVICE + SAVE_METHOD + LogConstant.IN + JSONUtil.toStr(in));
        DemoTable insert = new DemoTable();
        insert.setId(redisUtil.getAutoSeqId(DemoTable.AUTO_ID));
        insert.setCode(redisUtil.getAutoSeqCode(DemoTable.AUTO_CODE, DemoTable.AUTO_CODE_PREFIX));
        insert.setCreater("zhai");
        insert.setOperator("zhai");
        insert.setDemoJson(JSONUtil.toStr(in));
        demoDao.insert(insert);
        if ("翟".equals(in.getName())) {
            //回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        log.info(BUSINESS_NAME + LogConstant.RESULT + SAVE_METHOD + LogConstant.SUCCESS);
        return ServiceResult.success();
    }

    @Override
    public ServiceResult<Object> getById(String id) {
        log.info(BUSINESS_NAME + LogConstant.SERVICE + GET_BY_ID_METHOD + LogConstant.IN + JSONUtil.toStr(id));
        DemoTable demoGetResult = demoDao.getById(id);
        for (int i = 0; i < 20; i++) {
            sender.send();
        }
        log.info(BUSINESS_NAME + LogConstant.RESULT + GET_BY_ID_METHOD + LogConstant.SUCCESS + LogConstant.OUT + JSONUtil.toStr(demoGetResult));
        return ServiceResult.success(demoGetResult);
    }

    @Override
    public ServiceResult log(DemoLogIn in) {
        log.info(BUSINESS_NAME + LogConstant.SERVICE + LOG_METHOD + LogConstant.IN + JSONUtil.toStr(in));
        bizLog.save(new BizLogInOut(StringUtil.join(BizLogUtil.affix(BizLogUtil.FMT_MONTH_F), BizLogConstant.DEMO_FIND_TABLE), in, null));
        log.info(BUSINESS_NAME + LogConstant.RESULT + LOG_METHOD + LogConstant.SUCCESS);
        return ServiceResult.success();
    }
}
